import greenfoot.*;

public class MoveBckGr extends Actor
{
    private int x;          //Object's position in coordinate x.
    private int y;          //Object's position in coordinate y.
    private World w;  
    private int back;
    private int level;
    private Bird bird;
    private boolean edo;    //Flag of the accesories.
    private int distance;   //Distance from the man to the house. 

    /**
     * MoveBckGr Constructor.
     */
    public MoveBckGr( )
    {
        back = 0;
        edo = false;
        distance = 0;
        w = getWorld();     //This variable is to get the world's size.
    }

    /**
     * Method getAct return the flag of the actor.
     */
    public void getAct(boolean estado)
    {
        edo = estado;
    }

    /**
     * Method act.
     */
    public void act()
    {
    }    

    /**
     * It controls the movement of the objects on the scenario.
     */
    public int shift( int x, int y, int amount, int limit, int level)
    {   

        if( edo == true)
            back=Greenfoot.getRandomNumber(30);
        //if the player press the right arrow on the keyboard, and the man hasn´t got home...
        if( Greenfoot.isKeyDown( "right") && distance < 3470){
            distance = distance + 3;
            limits( x, y, 0 - amount, limit);}
        //if the player press the left arrow on the keyboard, and he isn´t at the begining...
        else
        if( (Greenfoot.isKeyDown( "left")|| back>0) && distance > 0){
            distance = distance - 3;
            back=back-3;
            limits( x, y, amount, limit);}
        if( back>0 && distance==0)
            back=0;
        return distance;
    }

    /**
     * Method limits changes the position of the objects out of the world.
     */
    public void limits(int x, int y, int amount, int limit)
    {
        w = getWorld();

        setLocation( x + amount, y);
        if( x < 0)                              //if the object crosses the limits allowed...
            setLocation( w.getWidth(), y);
        else
        if( x > w.getWidth() && limit != 1)
            setLocation( 0, y);
    }
}